package com.rbs.filter;

import com.rbs.entity.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter(urlPatterns = "/phone/*")
public class CheckPhoneLoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //servletRequest是HttpServletRequest的父类 里边没有getSession
        //需要向下类型强转
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        //获取session内容
        User user = (User) request.getSession().getAttribute("user");
        if (user != null){
            filterChain.doFilter(request,servletResponse);
        }else {
            ((HttpServletResponse)servletResponse).sendRedirect(request.getContextPath()+"/Login.jsp");
        }
    }

    @Override
    public void destroy() {

    }
}
